import matplotlib.pyplot as plt
import numpy as np
from scipy import integrate


# 占空比为25%的非对称周期
def signal0_real(t, k, T):
    x = (t >= T/4) * 1.0
    y = ((1/T) * x) * np.exp(-1j * (2 * np.pi / T) * k * t)
    return np.real(y)


def signal0_imag(t, k, T):
    x = (t >= T/4) * 1.0
    y = ((1/T) * x) * np.exp(-1j * (2 * np.pi / T) * k * t)
    return np.imag(y)


T = 1
N = 20
k = np.arange(-N, N + 1, 1)
N1 = len(k)
a1 = np.zeros(N1)
err1 = np.zeros(N1)
a2 = np.zeros(N1)
err2 = np.zeros(N1)

for n in range(N1):
    a1[n], err1[n] = integrate.quad(signal0_real, -T/2, T/2, args=(k[n], T))
    a2[n], err2[n] = integrate.quad(signal0_imag, -T/2, T/2, args=(k[n], T))


A = a1 + 1j * a2

plt.rcParams['font.sans-serif'] = ['SimHei']  # 加上这一句就能在图表中显示中文
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号


plt.subplot(311)
plt.stem(k, np.abs(A))
plt.ylabel('ak幅值')


plt.subplot(312)
plt.stem(k, a1)
plt.ylabel('ak实部')


plt.subplot(313)
plt.stem(k, a2)
plt.ylabel('ak虚部')

plt.show()
